import axios, { AxiosInstance } from 'axios'

const instance: AxiosInstance = axios.create({
  baseURL: '/api',
  headers: {
    'Content-Type': 'application/json',
  },
})

type BaseResponse<T = any> = {
  code: number
  message?: string
  param?: T
  data?: T
}

class phpApi {
  private axios: AxiosInstance

  constructor() {
    this.axios = instance
  }

  // 翻译文本
  async translate(text: string, from: string = 'zh', to: string = 'en') {
    try {
      const response = await this.axios.post<BaseResponse>('/webapi/translator.php', {
        from,
        to,
        input: text,
      })

      const result = response.data

      if (result.code !== 200) {
        throw new Error(result.message || 'Translation failed')
      }

      return result.data
    } catch (error) {
      console.error('Translation error:', error)
      throw error
    }
  }
}
export default new phpApi()
